package com.manor.paradise.activity

import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.interfaces.OnSelectListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.adapter.CrystalAdapter
import com.manor.paradise.adapter.OresAdapter
import com.manor.paradise.adapter.StartPointAdapter
import com.manor.paradise.bean.CrystalBean
import com.manor.paradise.bean.OresBean
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.bean.StartPointBean
import com.manor.paradise.dialog.PresentFriendsPopup
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.activity_crystal.*

/**
 * 米粒和能量
 */
@Suppress("MoveLambdaOutsideParentheses")
class CrystalActivity : AbsActivity(), View.OnClickListener {

    internal var pageNum = 1
    internal lateinit var oresAdapter: OresAdapter
    internal lateinit var startAdapter: StartPointAdapter
    internal lateinit var crystalAdapter: CrystalAdapter
    private var curPos = 1


    override fun getLayoutId(): Int {
        return R.layout.activity_crystal
    }

    internal var paradiseInfoBean: ParadiseInfoBean? = null
    override fun main() {
        super.main()
        im_back.setOnClickListener({ finish() })
        ivQuest.setOnClickListener({
            ExplainActivity.forward(this, ExplainActivity.CRYSTAL_DESCRIPTION)
        })
        tv_giveinfo.setOnClickListener({
            GiveInfoActivity.forward(this)
        })
        rcv_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

        oresAdapter = OresAdapter(R.layout.item_aura_value, null)
        startAdapter = StartPointAdapter(R.layout.item_aura_value, null)
        crystalAdapter = CrystalAdapter(R.layout.item_aura_value, null)
        tv_give.setOnClickListener(this)
        tv_jk.setOnClickListener(this)
        tv_xd.setOnClickListener(this)
        tv_sj.setOnClickListener(this)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
        initMode()
        pageNum = 1
        setData()
    }

    fun initMode() {
        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                paradiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                paradiseInfoBean?.run {
                    tv_crystal_total.text = crystalOre.toString()
                    tv_crystal_day.text = todayCrystalOre.toString()
                    tv_crystal_frozen.text = frozenCrystalOre.toString()
                }
            }

            override fun onError() {
                super.onError()

            }
        })
    }

    fun setData() {
        when (curPos) {
            1 -> {
                if (pageNum == 1) {
                    rcv_list.adapter = oresAdapter
                }

                ElvesHttpUtil.getOresList(pageNum, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }
                        var oresBean: OresBean = GsonUtil.gsonToBean<OresBean>(info, OresBean::class.java)
                        KLog.e("json", oresBean);
                        if (pageNum == 1) {
                            oresAdapter.setNewInstance(oresBean.records)
                            refreshLayout.finishRefresh()
                        } else {
                            oresAdapter.addData(oresBean.records)
                            refreshLayout.finishLoadMore()
                        }
                        if (oresAdapter?.data == null || oresAdapter.data.isEmpty()) {
                            oresAdapter?.setEmptyView(R.layout.view_empty_default)
                        }
                    }

                    override fun onError() {
                        super.onError()
                        refreshLayout.finishRefresh()
                        refreshLayout.finishLoadMore()
                    }
                })
            }
            2 -> {
                if (pageNum == 1) {
                    rcv_list.adapter = startAdapter
                }

                ElvesHttpUtil.getStartPointList(pageNum, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }
                        var oresBean: StartPointBean = GsonUtil.gsonToBean<StartPointBean>(info, StartPointBean::class.java)
                        KLog.e("json", oresBean);
                        if (pageNum == 1) {
                            startAdapter.setNewInstance(oresBean.records)
                            refreshLayout.finishRefresh()
                        } else {
                            startAdapter.addData(oresBean.records)
                            refreshLayout.finishLoadMore()
                        }
                        if (startAdapter?.data == null || startAdapter.data.isEmpty()) {
                            startAdapter?.setEmptyView(R.layout.view_empty_default)
                        }
                    }

                    override fun onError() {
                        super.onError()
                        refreshLayout.finishRefresh()
                        refreshLayout.finishLoadMore()
                    }
                })
            }
            3 -> {
                if (pageNum == 1) {
                    rcv_list.adapter = crystalAdapter
                }
                ElvesHttpUtil.getCrystalList(pageNum, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {
                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }
                        var crystalBean: CrystalBean = GsonUtil.gsonToBean<CrystalBean>(info, CrystalBean::class.java)
                        KLog.e("json", crystalBean);
                        if (pageNum == 1) {
                            crystalAdapter.setNewInstance(crystalBean.records)
                            refreshLayout.finishRefresh()
                        } else {
                            crystalAdapter.addData(crystalBean.records)
                            refreshLayout.finishLoadMore()
                        }
                        if (crystalAdapter?.data == null || crystalAdapter.data.isEmpty()) {
                            crystalAdapter?.setEmptyView(R.layout.view_empty_default)
                        }


                    }

                    override fun onError() {
                        super.onError()
                        refreshLayout.finishRefresh()
                        refreshLayout.finishLoadMore()

                    }
                })
            }
        }



    }

    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.tv_give -> {

                    val presentFriendsPopup = PresentFriendsPopup(this, object : OnSelectListener {
                        override fun onSelect(position: Int, text: String?) {
                            initMode()
                            pageNum = 1
                            setData()

                        }

                    })

                    XPopup.Builder(this)
                            .hasShadowBg(true)
                            .dismissOnTouchOutside(true)
                            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                            .asCustom(presentFriendsPopup)
                            .show()
                }
                R.id.tv_jk -> {
                    tv_jk.setCrystalSelect(true)
                    tv_xd.setCrystalSelect(false)
                    tv_sj.setCrystalSelect(false)

                    curPos = 1
                    setSelView()
                    pageNum = 1
                    setData()
                }
                R.id.tv_xd -> {
                    tv_jk.setCrystalSelect(false)
                    tv_xd.setCrystalSelect(true)
                    tv_sj.setCrystalSelect(false)

                    curPos = 2
                    setSelView()
                    pageNum = 1
                    setData()
                }
                R.id.tv_sj -> {
                    tv_jk.setCrystalSelect(false)
                    tv_xd.setCrystalSelect(false)
                    tv_sj.setCrystalSelect(true)
                    curPos = 3
                    setSelView()
                    pageNum = 1
                    setData()
                }
            }
    }

    private fun setSelView() {

        when (curPos) {
            1 -> {
                paradiseInfoBean?.run {
                    tv_name.text = "米粒明细"
//                    tv_give.visibility = View.VISIBLE
//                    tv_giveinfo.visibility = View.VISIBLE
                    tv_crystal_total.setTextColor(Color.parseColor("#FF5400"))
                    tv_zs.text = "米粒总数"
                    tv_crystal_total.text = crystalOre.toString()

                    tv_crystal_day.setGone(false)
                    tv_cz.setGone(false)
                    tv_crystal_day.setTextColor(Color.parseColor("#FF5400"))
                    tv_cz.text = "当天产值"
                    tv_crystal_day.text = todayCrystalOre.toString()

                    tv_crystal_frozen.visibility = View.VISIBLE
                    tv_dj.visibility = View.VISIBLE
                    tv_crystal_frozen.text = frozenCrystalOre.toString()
                }
            }
            2 -> {
                paradiseInfoBean?.run {
                    tv_name.text = "米壳明细"
//                    tv_give.visibility = View.GONE
//                    tv_giveinfo.visibility = View.GONE
                    tv_crystal_total.setTextColor(Color.parseColor("#FF5400"))
                    tv_zs.text = "米壳总数"
                    tv_crystal_total.text = startPoint.scaleDown(4).toString()

                    tv_crystal_day.setGone(true)
                    tv_cz.setGone(true)

                    tv_crystal_frozen.visibility = View.GONE
                    tv_dj.visibility = View.GONE
                }
            }
            3 -> {
                paradiseInfoBean?.run {
                    tv_name.text = "能量明细"
//                    tv_give.visibility = View.GONE
//                    tv_giveinfo.visibility = View.GONE
                    tv_crystal_total.setTextColor(Color.parseColor("#FF5400"))
                    tv_zs.text = "能量总数"
                    tv_crystal_total.text = crystal.scaleDown(4).toString()

                    tv_crystal_day.setGone(false)
                    tv_cz.setGone(false)
                    tv_crystal_day.setTextColor(Color.parseColor("#FF5400"))
                    tv_cz.text = "当天所得"
                    tv_crystal_day.text = todayCrystal.toString()

                    tv_crystal_frozen.visibility = View.GONE
                    tv_dj.visibility = View.GONE
                }
            }
        }
    }

    private fun TextView.setCrystalSelect(boo: Boolean){
        if (boo){
            this.background = DrawableUtil.getDrawable(8, "#F3951D")
        }else{
            this.background = DrawableUtil.getDrawable(8, "#998E99")
        }
    }

    override fun onDestroy() {
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_DETAILS_LIST)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_ORE_LIST)

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        super.onDestroy()
    }

}